package com.example.HelloSpace.ui;


import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

import com.example.HelloSpace.adapters.CarouselViewAdapter;
import com.example.HelloSpace.R;
import com.example.HelloSpace.utils.ViewUtils;

import java.util.List;


public class CarouselView extends FrameLayout {

    public ViewPager2 viewPager;
    private Handler handler = new Handler();
    private Runnable runnable;
    private int autoPlayInterval = 5000; // 默认5秒

    public CarouselView(@NonNull Context context) {
        this(context, null);
    }

    public CarouselView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CarouselView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_carousel, this, true);
        post(()->{
            ViewUtils.resizeViewByWidth(context,this,0.90f,0.50f);
        });
        viewPager = findViewById(R.id.viewPager);
        // 默认不启动自动播放，等待设置图片列表后再启动
    }

    // 设置图片URL列表并启动自动播放
    public void setImageUrls(List<String> imageUrls) {
        if (imageUrls == null || imageUrls.isEmpty()) return;

        CarouselViewAdapter adapter = new CarouselViewAdapter(imageUrls, viewPager);
        viewPager.setAdapter(adapter);

        startAutoPlay();
    }

    // 启动自动播放
    public void startAutoPlay() {
        stopAutoPlay(); // 先停止之前的定时任务
        setCurrentItem(viewPager.getCurrentItem(), false);
        if (viewPager.getAdapter() != null && viewPager.getAdapter().getItemCount() > 1) {
            runnable = () -> {
                int currentItem = viewPager.getCurrentItem();
                int totalItems = viewPager.getAdapter().getItemCount();
                if(currentItem==totalItems-1)
                    viewPager.setCurrentItem(1, false);
                else{
                    viewPager.setCurrentItem(currentItem + 1, true);
                }
                handler.postDelayed(runnable, autoPlayInterval);
            };
            handler.postDelayed(runnable, autoPlayInterval);
        }
    }

    // 停止自动播放
    private void stopAutoPlay() {
        handler.removeCallbacks(runnable);
    }

    // 设置自动播放间隔
    public void setAutoPlayInterval(int interval) {
        this.autoPlayInterval = interval;
        setCurrentItem(viewPager.getCurrentItem(), false);
        // 如果已经启动了自动播放，则重新启动以应用新的间隔
        if (runnable != null) {
            startAutoPlay();
        }
    }

    // 当宿主Activity或Fragment销毁时，确保停止自动播放以防止内存泄漏
    public void onDestroy() {
        stopAutoPlay();
    }

    public void setCurrentItem(int startPosition, boolean b) {
        viewPager.setCurrentItem(startPosition, b);
    }
}
